﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTVNEW.Properties;
namespace QLTVNEW
{
    public partial class frmDangNhap : DevComponents.DotNetBar.Office2007Form
    {
        public frmDangNhap()
        {
            InitializeComponent();
        }
        QLTVDataContext db = new QLTVDataContext();

        #region 'Delegate cho form Main'
        public void GetValue(string vl1, string vl2)
        {
            txtuser.Text = vl1;
            txtpass.Text = vl2;
        }

        public delegate void PassData(string value);

        void OpenForm()
        {
            System.Windows.Forms.Application.Run(new frmMain());
        }
        #endregion

        private void frmDangNhap_Load(object sender, EventArgs e)
        {
            txtuser.Focus();
            txtuser.Text = Settings.Default["Name"].ToString();
            txtpass.Text = Settings.Default["Pass"].ToString();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
           
            frmSearch frm = new frmSearch();
            frm.Show();
           
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            frmSearchDocGia frm = new frmSearchDocGia();
            frm.Show();
        }

        private void btnDN_Click(object sender, EventArgs e)
        {
            if (txtuser.Text!="" && txtpass.Text!="")
            {
                Account tb = db.Accounts.Where(a => a.UserName == txtuser.Text && a.Pass == txtpass.Text).SingleOrDefault<Account>();
                if (tb != null)
                {
                    this.Hide();
                    frmMain frm = new frmMain();
                    PassData pass = new PassData(frm.GetValue);
                    pass(this.txtuser.Text);
                    frm.Show();
                    if (checkBox1.Checked==true)
                    {
                        Settings.Default["Name"] = txtuser.Text;
                        Settings.Default["Pass"] = txtpass.Text;
                        Settings.Default.Save();
                    }
                    else
                    {
                        Settings.Default["Name"] = "";
                        Settings.Default["Pass"] = "";
                        Settings.Default.Save();
                    }
                    frm.ribbonBar1.Visible = true;
                    frm.ribbonTabItem2.Visible = true;
                    frm.ribbonTabItem4.Visible = true;
                    frmDangNhap frmdn = new frmDangNhap();
                    frmdn.Close();
                }
                else
                    MessageBox.Show("Thông Tin Đăng Nhập Sai", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
                MessageBox.Show("Phải Nhập Đầy Đủ Thông Tin","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void frmDangNhap_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Settings.Default["Name"] = "";
            Settings.Default["Pass"] = "";
            Settings.Default.Save();
            frmMain frm = new frmMain();
            frm.ShowDialog();
            this.Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                db.Connection.Open();
                int rowAffected = db.ExecuteCommand("USE MASTER EXECUTE  sp_attach_db 'QuanLyTV','" + Application.StartupPath + @"\BackUp\QuanLyTV.mdf" + "','" + Application.StartupPath + @"\BackUp\QuanLyTV_log.ldf'");
                if (rowAffected != 0)
                {
                    MessageBox.Show("Attach Database Quản Lý Thư Viện Thành Công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    db.SubmitChanges();
                    Application.Restart();
                }
                else
                {
                    MessageBox.Show("Attach Database Quản Lý Thư Viện Thất Bại!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                  

            }
            catch (System.IO.IOException ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Thư Viện", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản Lý Thư Viện", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                if (db.Connection.State == ConnectionState.Open)
                {
                    db.Connection.Close();
                }
            }
        }
    }
}
